
import 'package:dio/dio.dart';
import 'package:factoryside/api/api.dart';
import 'package:factoryside/utils/utils.dart';
import 'package:factoryside/widget/LoadingDialog.dart';
import 'package:flutter/material.dart';

import 'JsonUtils.dart';


class RequestUtil {
  static BuildContext _context;
  static bool isShowing=false;
  static Dio _dio;
  static InterceptorsWrapper interceptorsWrapper = InterceptorsWrapper(onRequest: (RequestOptions options,RequestInterceptorHandler handler) {
//        print('=============================request============================');
//        print('url:${options.uri}');
//        print('method ${options.method}');
//        print('headers:${options.headers}');
//        print('data:\n${options.data}');
//        print('queryParameters:\n${options.queryParameters}');
    JsonUtil.printJson(options.headers);
    JsonUtil.printJson(options.data);
    return handler.next(options);
  }, onResponse: (Response response,ResponseInterceptorHandler handler) {
    JsonUtil.printRequest(response);
    JsonUtil.printRespond(response);
//        print('=============================response============================');
//        print('url:${response.request.uri}');
//        debugPrint('response:${response.data}');
    return handler.next(response);
  }, onError: (DioError e,ErrorInterceptorHandler handler) {
    print('Error url:${e.requestOptions.uri}');
    switch(e.type){
      case DioErrorType.connectTimeout:
        showToast('网络连接超时，请稍后再试');
        break;
      case DioErrorType.sendTimeout:
        showToast('网络请求超时，请稍后再试');
        break;
      case DioErrorType.receiveTimeout:
        showToast('网络请求超时，请稍后再试');
        break;
      case DioErrorType.response:
//            showToast('服务器错误,${e.error}');
        break;
      case DioErrorType.cancel:
        showToast('请求已取消');
        break;
      case DioErrorType.other:
        showToast('网络错误，请检查网络连接或稍后再试');
        break;
    }
    hiddenLoadingDialog(_context);
    return handler.next(e);
  });
  static Dio getInstance() {
    if (_dio == null) {
      BaseOptions options = new BaseOptions(
        baseUrl: Api.baseUrl,
        connectTimeout: 10000,
        receiveTimeout: 10000,
      );
      _dio = new Dio(options);
//      _dio.interceptors.add(LogInterceptor(
//        request : false,
//        requestHeader : false,
//        requestBody : true,
//        responseHeader : false,
//        responseBody : true,
//      )); //开启请求日志
      _dio.interceptors.add(interceptorsWrapper);
    }
    return _dio;
  }
  ///请求api
  static Future<Map> request(String url,{data,method}) async{
    data = data ?? {};
    method = method ?? "get";
    var dio = getInstance();
    dio.options.headers={
      "userName":Api.userName,
      "adminToken":Api.adminToken,
      "IdToken":Api.formToken,
    };
    var res;
    if(method=="get"){
      var response =await dio.get(url);
      res=response.data;
    }else if(method=="post"){
      var response=await dio.post(url,data: data);
      res=response.data;
    }
//    print(res.toString());
    return res;
  }
  ///get
  static Future<Map> get(url,data)=>request(url,data:data);
  ///post
  static Future<Map> post(url,data)=>request(url,data: data,method: "post");
  static showLoadingDialog(BuildContext context){
    _context=context;
    isShowing=true;
    showDialog<Null>(
        context: context,
        barrierDismissible: true,
        builder: (context) {
          return LoadingDialog(
            isCancel: true,
          );
        });
  }
  static hiddenLoadingDialog(BuildContext context){
    if(context==null)return;
    if (isShowing) {
      Navigator.pop(context);
    }
    isShowing=false;
  }
}
